import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DataGtid extends StatefulWidget {
  List dataList;

  String? title;

  DataGtid(
      {Key? key,
      required this.dataList,
      required this.title,})
      : super(key: key);

  @override
  _DataGtidState createState() => _DataGtidState();
}

class _DataGtidState extends State<DataGtid> {
  late DataSource _dataSource;
  late Timer timer;
  final textStyle = TextStyle(
      color: Color.fromRGBO(11, 123, 118, 1.0),
      decoration: TextDecoration.none,
      fontSize: 18.sp);

  @override
  Widget build(BuildContext context) {
    return Container(padding: EdgeInsets.all(10),child: _buildDataGrid(),);
  }

  @override
  void initState() {
    super.initState();
    _dataSource = DataSource(widget.dataList);
  }

  @override
  void didUpdateWidget(DataGtid oldWifget){
    _dataSource = DataSource(widget.dataList);
  }

  @override
  void dispose() {
    super.dispose();
  }


  _buildDataGrid() {
    return Column(
      children: [
        Container(
          alignment: Alignment.center,
          color: Color.fromRGBO(48, 48, 85, 1.0),
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  widget.title!,
                  style: TextStyle(
                      color: Color.fromRGBO(255, 255, 255, 1),
                      decoration: TextDecoration.none,
                      fontSize: 25.sp),
                ),
              ],
            ),
          ),
        ),
        Expanded(
          // width: width,
          // height: 200.h,
          child: SfDataGridTheme(
              data: SfDataGridThemeData(
                gridLineColor: Color.fromRGBO(46, 47, 83, 1.0),
                headerColor: Colors.transparent,
                headerHoverColor: Colors.transparent,
              ),
              child: SfDataGrid(
                columnWidthMode: ColumnWidthMode.fill,
                source: _dataSource,
                rowHeight: 55.h,
                headerRowHeight: 55.h,
                gridLinesVisibility: GridLinesVisibility.both,
                headerGridLinesVisibility: GridLinesVisibility.both,
                columns: [
                  GridColumn(
                      columnName: 'no',
                      maximumWidth: 100.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '序号',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'productName',
                      // minimumWidth: 270.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '物料名称',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'qtyDone',
                      maximumWidth: 130.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '数量',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'jobType',
                      maximumWidth: 130.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '类型',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                  GridColumn(
                      columnName: 'writeDate',
                      minimumWidth: 250.r,
                      maximumWidth: 300.r,
                      label: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.0),
                          alignment: Alignment.center,
                          child: Text(
                            '操作时间',
                            style: textStyle,
                            overflow: TextOverflow.ellipsis,
                          ))),
                ],
              )),
        ),
      ],
    );
  }
}

class DataSource extends DataGridSource {
  DataSource(List dataList) {
    var index = 1;
    dataGridRows = dataList
        .map<DataGridRow>((data) => DataGridRow(cells: [
              DataGridCell<int>(columnName: 'no', value: index++),
              DataGridCell<String>(columnName: 'productName', value: data['product_id']['pro_name']), //物料名称
              DataGridCell<String>(columnName: 'qtyDone', value: data['qty_done']!=false?data['qty_done'].toString():'0'), //物料名称
              DataGridCell<String>(columnName: 'jobType', value: data['job_type']!=false?data['job_type'][1]:''), //物料名称
              DataGridCell<String>(columnName: 'writeDate', value: Utils.timeUTC(data['write_date'])), //单价
    ]))
        .toList();
  }

  List<DataGridRow> dataGridRows = [];

  @override
  List<DataGridRow> get rows => dataGridRows;

  @override
  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        color: Colors.transparent,
        cells: row.getCells().map<Widget>((dataGridCell) {
          return Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Text(
                    dataGridCell.value.toString(),
                    style: TextStyle(
                        fontSize: 18.sp,
                        color: Colors.white,
                        decoration: TextDecoration.none),
                    overflow: TextOverflow.ellipsis,
                  ));
        }).toList());
  }
}
